﻿--[[
   @brief    login_popup_tips_view
   @author   maxyao, charleycao
   @version  1.0
   @date     2017-03-23
--]]

login_popup_tips_view =
{
    init = function()
        ierd_tgp.tpf_ui.Require("login_popup_tips\\control.lua");
        ierd_tgp.tpf_ui.Require("login_popup_tips\\model.lua");
        ierd_tgp.tpf_ui.Log("[login_popup_tips_view] view init.");
    end;


    ready = function ()
        login_popup_tips_model.show_dlg = true;
        local nDlg = ui_utility.get_ctrl_id_by_cid(CID_LOGIN_POPUP_TIPS_DLG);
        if nDlg == Tenio.INVALID_CONTROLID then
            login_popup_tips_model.dlg_id = layout_mgr.restore_layout("login_popup_tips.tpf");
            login_popup_tips_model.window_exists = true;
        end;
        
        local pDlg = ui_utility.get_ctrl_ptr_by_cid(CID_LOGIN_POPUP_TIPS_DLG);
        if (nil == pDlg) then
            login_popup_tips_model.show_dlg = false;
            -- TODO: LOG ERROR
            -- base_utility.log("[login_popup_tips_view] show login popup tips.");
            return;
        end;
        login_popup_tips_model.dlg_ptr = pDlg;

        login_popup_tips_view.update_native_dlg();
        login_popup_tips_view.load_web_page();
       
        base_utility.log("[login_popup_tips_view] ready login popup tips.");        
    end;

    show = function() 
        if base_utility.is_big_picture_ui() then
            base_utility.log(
            "[login_popup_tips_view] in big pic ui, not show login popup tips.", true);
            return;
        end;

        if (nil == login_popup_tips_model.window_is_show) then
            login_popup_tips_model.window_is_show = true;
            login_popup_tips_view.show_page();      
            base_utility.log("[login_popup_tips_view] show login popup tips.");
        end;

    end;


    update_native_dlg =  function ()
        --[[local index_id = ui_utility.get_ctrl_id_by_path(login_popup_tips_model.dlg_id,
                         "title_bar\\LayoutTitle\\Container_num\\ad_index");
        local slash_id = ui_utility.get_ctrl_id_by_path(login_popup_tips_model.dlg_id,
                         "title_bar\\LayoutTitle\\Container_num\\ad_slash");
        local count_id = ui_utility.get_ctrl_id_by_path(login_popup_tips_model.dlg_id,
                         "title_bar\\LayoutTitle\\Container_num\\ad_count");
        if (Tenio.INVALID_CONTROLID == index_id or Tenio.INVALID_CONTROLID == count_id) then
            login_popup_tips_model.show_dlg = false;
            -- TODO: LOG ERROR
            return;
        end;
        local index = login_popup_tips_model.tips_page_curr;
        local count = login_popup_tips_model.tips_page_count;
        -- 只有多于1条广告的时候才显示
        if count > 1 then
            ui_utility.setproperty_by_id(index_id, "Caption", tostring(index));
            ui_utility.setproperty_by_id(count_id, "Caption", tostring(count));
            -- 显示数字
            ui_utility.setproperty_by_id(index_id, "Visible", "true");
            ui_utility.setproperty_by_id(slash_id, "Visible", "true");
            ui_utility.setproperty_by_id(count_id, "Visible", "true");
            base_utility.log("[login_popup_tips_view] update_native_dlg, show "..index.."/"..count);
        else
            -- 隐藏数字
            ui_utility.setproperty_by_id(index_id, "Visible", "false");
            ui_utility.setproperty_by_id(slash_id, "Visible", "false");
            ui_utility.setproperty_by_id(count_id, "Visible", "false");
            base_utility.log("[login_popup_tips_view] update_native_dlg, hide "..index.."/"..count);
        end--]]
    end;

    reset_dislike_detail =  function ()
        comm_center.send_broadcast(login_popup_tips_model.reset_dislike_detail_broadcast, {});
    end;

    load_web_page = function()
        base_utility.log("[login_popup_tips_view] load_web_page");
        local pControl = ui_utility.get_ctrl_ptr_by_cid(CID_LOGIN_POPUP_TIPS_QBWEB);
        if nil == pControl then
            login_popup_tips_model.show_dlg = false;
            -- TODO: LOG ERROR
            return;        
        end;
        local pQBWeb = tolua.cast(pControl, "Tenio::IQBWebBrowser");
        local url = login_popup_tips_model.page_url;
        local const_ui_str = U8toCUIStr(url);
        pQBWeb:Navigate(const_ui_str);
    end;

    show_page = function()
        local pDlg = login_popup_tips_model.dlg_ptr;
        if nil == pDlg then
            login_popup_tips_model.show_dlg = false;
            -- TODO: LOG ERROR  
            return;        
        end;     
        pDlg:MoveToCenter(); 
        pDlg:Show(TRUE);
        pDlg:BringToTop();
        pDlg:SetFocus();
        --shadow_helper.create_shadow(login_popup_tips_model.dlg_id);

        base_utility.log("[login_popup_tips_view] show_page");
        --TODO report 
    end;


    minimize = function(uilib, dwID_Click)
        local control_id = uilib:GetControlIDbyCustomID(CID_LOGIN_POPUP_TIPS_DLG);
        local control = uilib:GetControlPtr(control_id);
        local component = tolua.cast(control, "Tenio::ITDialog");
        component:Minimize();
    end;

    close = function(uilib, id)
        local pDlg = login_popup_tips_model.dlg_ptr;
        if nil == pDlg then
            login_popup_tips_model.show_dlg = false;
            -- TODO: LOG ERROR     
            return;     
        end;   
        pDlg:Show(FALSE);
        local nDlg = login_popup_tips_model.dlg_id;
        if Tenio.INVALID_CONTROLID ~= nDlg then
            --shadow_helper.destroy_shadow(nDlg);
            layout_mgr.delay_remove_ctrl(nDlg);
            login_popup_tips_model.dlg_id = Tenio.INVALID_CONTROLID;
        end;
        base_utility.log("[login_popup_tips_view] close dlg");
    end;



};

login_popup_tips_view.init();